<?php
header("content-type:text/html;charset=utf-8");
echo '<style type="text/css">a:visited{color: green;}</style>';
//获取文件内容
function getcon($filename){
	$handle = fopen($filename, "a+");
	$content=fread($handle, filesize($filename));
?>
	<form action="#" method="post">
		<input type="hidden" name="create" value="create" />
		文件名：<input name="filename" type="text" value="<?php echo $filename; ?>" style="width: 80%;"/>
		<p>内容：</p>
		<textarea style="width: 100%; height: 500px;" name="str" rows="10" cols="50"><?php echo $content; ?></textarea>
		<!--<input type="submit" name="submit" value="提 交"   />-->
		<!--<input type="reset" value="重 置" name="reset"  />-->
	</form>
<?php
	exit();	
}
//传入文件名
if(!empty($_GET['gfname'])){
	getcon($_GET['gfname']);
	exit();
}

//匹配方法
$arrall=array();
function preg($filename){
	global $arrall;
	global $pattern;
	$handle = fopen($filename, "r");
	$subject = fread($handle, filesize($filename));
	$preg=preg_match($pattern, $subject);
	if($preg==0){
		echo "没有匹配到";
	}else{
		echo "<span style='color: red;'>有匹配的".$pattern."</span>"."<a style='border: 2px solid red;' target='_blank' href='preg-seefile.php?gfname=".$filename."'>打开文件</a>";
		$arrall[] =$filename."<a style='border: 2px solid red;' target='_blank' href='preg-seefile.php?gfname=".$filename."'>打开文件</a>";
	}
}
$row = $dir = $html = $php = $js = $css = $png = $jpg = $txt = $other = 0;
//递归目录
function getdir($path) {
	global $row,$dir,$html,$php,$js,$css,$png,$jpg,$txt,$other;
	$arr = scandir($path);
	foreach ($arr as $v) {
		if ($v != "." && $v != "..") {
			if (is_dir($path . "/" . $v)) {
				$row++;
				$dir++;
				echo "<hr>";
				echo $row."、目录：" . $path . "/" . $v;
				echo "<br>";
				getdir($path . "/" . $v);
			} else {
				$row++;
				echo $row."、文件：" . $path . "/" . $v;
				$ext = substr($v, (strrpos($v, ".") + 1));
				if ($ext == 'html' ) {
					$html++;
					preg($path . "/" . $v);
				} elseif ($ext == 'php') {
					$php++;
					preg($path . "/" . $v);
				} elseif ($ext == 'js') {
					$js++;
					preg($path . "/" . $v);
				} elseif ($ext == 'css') {
					$css++;
					preg($path . "/" . $v);
				} elseif ($ext == 'png') {
					$png++;
					echo "==》》这是一个png图片不用匹配！";
				} elseif ($ext == 'jpg') {
					$jpg++;
					echo "==》》这是一个jpg图片不用匹配！";
				} elseif ($ext == 'txt') {
					$txt++;
					preg($path . "/" . $v);
				} else {
					$other++;
//					echo "==》》这是一个不用匹配的其他文件！";
					preg($path . "/" . $v);
				}
				echo "<br>";
			}
		}
	}
}

$dirname = $_POST['webname'];
$str=$_POST['str']?$_POST['str']:1;
$pattern = '/'.$_POST['str'].'/';
if (!empty($_POST['webname'])) {
	echo $dirname;
	echo "<hr>";
	getdir($dirname);

	echo "<hr><hr>";
	echo "本次匹配统计：递归文件夹" . $dir . "个！" ;
	echo "<br>";
	echo "遍历的文件总计:" . ($html+$php+$js+$css+$png+$jpg+$txt+$other) . "个!";
	echo "<br>";
	echo "其中：";
	echo "<br>";
	echo "遍历的HTML文件" . $html . "个!";
	echo "<br>";
	echo "遍历的PHP文件" . $php . "个!";
	echo "<br>";
	echo "遍历JS文件" . $js . "个!";
	echo "<br>";
	echo "遍历CSS文件" . $css . "个!";
	echo "<br>";
	echo "遍历png图片文件" . $png . "个!";
	echo "<br>";
	echo "遍历jpg图片文件" . $jpg . "个!";
	echo "<br>";
	echo "遍历txt文件" . $txt . "个!";
	echo "<br>";
	echo "遍历的其他文件" . $other . "个！<br>";
	echo "<hr>";
	echo "以下文件里有匹配的类容：";
	echo "<pre>";
	print_r($arrall);
	echo "</pre>";
	echo "匹配".$pattern."<br />";
	echo "本次成功匹配".count($arrall)."次！";
}
?>
<html>
	<head>
		<meta charset="utf-8">
		<title>递归匹配</title>
	</head>
	<body>
		<form action="#" method="post">
			<br />
			<br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入要匹配的文件夹：
			<input type="text" name="webname" id="webname" value="<?php echo $dirname;?>" /> <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入要匹配的字符串：
			<input type="text" name="str" id="str" value="<?php echo $str;?>" /> <br />
			<input style="margin-left: 300px;margin-top: 12px;" type="submit" value="开始匹配"/>
		</form>
		<br />
		<br />
		<h3>注：输入本程序文件的相对目标文件夹</h3>
		<br />
		<br />
	</body>
</html>